package D12_4z;

import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) throws Exception {
        System.out.println("请输入字符串<-1为null>:");
        String string = new Scanner(System.in).nextLine();
        if (string.equals("-1")){
            System.out.println(stringToNumber(null));
            return;
        }
        System.out.println(stringToNumber(string));
    }
    public static int stringToNumber(String s) throws Exception {
        int anInt = 0;
        if (s==null){
            throw new Exception("字符串不能为null");
        }
        if (s.equals("")){
            throw new Exception("字符串不能为空");
        }
        try {
            anInt = Integer.parseInt(s);
        } catch (NumberFormatException e) {
            throw new Exception("字符串转换失败");
        }
        return anInt;
    }
}
